package com.fxb.stream.demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 演示 stream 的简单使用
 *
 * @author fangjiaxiaobai
 * @date ${YEAR}-${MONTH}-${DAY} ${HOUR}:${MINUTE}
 */
public class SimpleDemo1 {

    public static void main(String[] args) {
        Stream<Integer> integerStream = Stream.of(1, 2, 3);
        // do somethings ..

        // Collection.stream();
        List<Integer> list = new ArrayList<>();
        List<Integer> collect = list.stream()
                .map(item -> item + 5)
                .filter(item -> item > 10)
                .sorted()
                .limit(10)
                .collect(Collectors.toList());


        for (int i = 0; i < list.size(); i++) {
            Integer integer = list.get(i);
            integer += 5;
        }
        // 正序排序
//        sort(list);

        // 创建新List.存储10个元素
        List<Integer> newList = new ArrayList<>();
        if (list.size() > 10) {
            System.arraycopy(list, list.size() - 11, newList, 0, 10);
        } else {
            System.arraycopy(list, 0, newList, 0, list.size());
        }

    }
}
